package moro;

/**
 * Title:        The MObile RObot Simulation Environment
 * Description:  The laser accepts commands from the controller and executes them in steps.
 *               The most important tasks are rotating the laser and getting measurements
 *               from the environment.
 * Copyright:    Copyright (c) 2001
 * Company:      Universito di Bergamo
 * 
 * @author Davide Brugali, Henk Bleker
 * @version 1.0
 */

import java.awt.Color;
import java.awt.Polygon;
import java.awt.geom.Point2D;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Laser extends Sensors{
	int range = 100;      // maximum range Laser

	public Laser(String name, Robot robot, Position localPos, Environment environment) {
            super(name, robot, localPos, environment);
            bgColor = Color.RED;
            this.addPoint(0, 2);
            this.addPoint(100, 2);
            this.addPoint(100, -2);
            this.addPoint(0, -2);
	}
        
        /**
         * draws the device's geometric shape on the graphical interface
         * @param g
         */
        @Override
        public void paint(Graphics g) {
            if(running) {
		// reads the robot's current position
		robot.readPosition(robotPos);
		
                // draws the shape
		Polygon globalShape = new Polygon();
		Point2D point       = new Point2D.Double();
		
                for(int i=0; i < shape.npoints; i++) {
                    point.setLocation(shape.xpoints[i], shape.ypoints[i]);
                    // calculates the coordinates of the point according to the local position
                    localPos.rototras(point);
                    // calculates the coordinates of the point according to the robot position
                    robotPos.rototras(point);
                    // adds the point to the global shape
                    globalShape.addPoint((int)Math.round(point.getX()),(int)Math.round(point.getY()));
		}

                ((Graphics2D) g).setColor(bgColor);
		((Graphics2D) g).fillPolygon(globalShape);
		((Graphics2D) g).setColor(fgColor);
		((Graphics2D) g).drawPolygon(globalShape);
            }
        }
        
        @Override
        public boolean isOpaqueObstacle(Obstacle obstacle){
            if(obstacle.opaque) {
                return true;
            }
            return false;
        }
        
    /**
     * 
     * @return range of laserbeam
     */
    @Override
    public int getRange() {
        return range;
    }

}
